package com.wsc.wisdomchina.ai.adapter;

import android.content.Context;
import android.content.Intent;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import com.samton.utils.ToastUtil;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.ai.GradePopUtil;
import com.wsc.wisdomchina.ai.IBenDemoChatActivity;
import com.wsc.wisdomchina.ai.MyConstant;
import com.wsc.wisdomchina.application.MyApplication;


/**
 * Created by liuhaigang on 2016/12/14.
 */
public class GridViewAdapter extends BaseAdapter {
    ///微笑/呲牙/憨笑/偷笑/鼓掌/敲打/晕/冷汗/惊讶
    //撇嘴/酷/疑问/奋斗/流泪/可怜/害羞/擦汗/困/衰
//    * /恐惧/发呆/大兵/猪头/流汗/抓狂/阴险/色/坏笑
//    * /傲慢/白眼/鄙视/咒骂/发怒
    private String[] picName = new String[]{"/微笑", "/呲牙", "/憨笑", "/偷笑", "/鼓掌", "/敲打", "/晕", "/冷汗", "/惊讶", "/撇嘴",
            "/酷", "/疑问", "/奋斗", "/流泪", "/可怜", "/害羞", "/擦汗", "/困", "/衰", "/恐惧", "/发呆", "/大兵", "/猪头", "/流汗", "/抓狂",
            "/阴险","/色", "/坏笑", "/傲慢", "/白眼", "/鄙视", "/咒骂", "/发怒"};
    private String [] titleName=new String [] {"拍照","相册","评分"};
    private Context context;
    private int[] mpic = null;
    private int type;

    public GridViewAdapter(Context context, int[] mpic, int type) {
        this.context = context;
        this.mpic = mpic;
        this.type = type;
    }

    @Override
    public int getCount() {

        return mpic == null ? 0 : mpic.length;
    }

    @Override
    public Object getItem(int position) {
        return mpic == null ? null : mpic[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        final IBenDemoChatActivity mActivity = (IBenDemoChatActivity) context;
        if (convertView == null) {
            holder = new ViewHolder();
            if (type == 1) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_emoji_pic, parent, false);
                holder.mImageView = (ImageView) convertView.findViewById(R.id.itemImage);
            } else {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_photo_pic, parent, false);
                holder.mImageView = (ImageView) convertView.findViewById(R.id.itemImage);
                holder.mItemTextView=((TextView) convertView.findViewById(R.id.itemTitle));
            }
            // 显示图片
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (type == 1) {
            Glide.with(context).load(mpic[position]).into(holder.mImageView);
//            holder.mImageView.setImageResource(mpic[position]);
            holder.mImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText editText = (EditText) ((IBenDemoChatActivity) context).findViewById(R.id.mEditText);
                    String string = editText.getText().toString().trim();
                    // 添加表情描述文字
                    editText.setText(string + picName[position]);
                }
            });
        } else {
            holder.mItemTextView.setText(titleName[position]);
            // 显示图片
            holder.mImageView.setBackgroundResource(mpic[position]);
            holder.mImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!MyConstant.isIBen) {
                        if (position == 0) {
                            Intent mIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            mActivity.startActivityForResult(mIntent, 200);
                        } else if (position == 1) {
                            Intent intent = new Intent();
                            intent.setAction(Intent.ACTION_PICK);
                            intent.setType("image/*");
                            mActivity.startActivityForResult(intent, 100);
                        }
                        else if (position == 2){
                            if (MyApplication.getSPValue()){
                                ToastUtil.showToast("已经做出评分~");
                            }else {
                                GradePopUtil popUtil = new GradePopUtil(context,((IBenDemoChatActivity) context).popView);
                                popUtil.show();
                                ((IBenDemoChatActivity) context).mChooseLay.setVisibility(View.GONE);
                            }
                        }
//                        else {
//                            Intent intent = new Intent();
//                            intent.setAction(Intent.ACTION_GET_CONTENT);
//                            intent.setType("text/*");
//                            mActivity.startActivityForResult(intent, 300);
//                        }
                    }else{
                        ToastUtil.showToast("小笨不支持发送图片，文件和评分");
                    }

                }
            });
        }
        return convertView;
    }

    class ViewHolder {
        ImageView mImageView;
        TextView mItemTextView;
    }
}
